/**
 * 
 */
package com.demo2do.strawberry.entity.user;

import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import com.demo2do.strawberry.entity.user.member.Member;
import com.demo2do.strawberry.entity.user.profile.Profile;

/**
 * @author lute
 *
 */
@Entity
@Table(name = "user_register")
public class UserRegister implements UserDetails {
	
	private static final long serialVersionUID = 7218754052137793128L;

	@Id
	@GeneratedValue
	private Long id;
	
	private String email;
	
	private String password;
	
	@ManyToOne(fetch = FetchType.EAGER, optional = false)
	private Member member;
	
	@Transient
	private Profile profile;

	private boolean enabled;
	
	/**
	 * default constructor
	 */
	public UserRegister() {
		
	}
	
	/**
	 * initialize UserRegister
	 * 
	 * @param member
	 * @param password
	 * @return
	 */
	public UserRegister initialize(Member member, String password) {
		this.member = member;
		this.password = password;
		this.enabled = false;
		return this;
	}
	
	/**
	 * 
	 * @param profile
	 */
	public void initialize(Profile profile) {
		this.profile = profile;
	}
	
	/**
	 * encode password
	 * 
	 * @param encodedPassword
	 * @return
	 */
	public UserRegister encodePassword(String encodedPassword) {
		this.password = encodedPassword;
		return this;
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#getUsername()
	 */
	public String getUsername() {
		return email;
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
	 */
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return AuthorityUtils.createAuthorityList(this.member.getAuthority());
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isEnabled()
	 */
	public boolean isEnabled() {
		return this.enabled;
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isCredentialsNonExpired()
	 */
	public boolean isCredentialsNonExpired() {
		return true;
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked()
	 */
	public boolean isAccountNonLocked() {
		return true;
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired()
	 */
	public boolean isAccountNonExpired() {
		return true;
	}
	
	/**
	 * @return the profile
	 */
	public Profile getProfile() {
		return profile;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @return the member
	 */
	public Member getMember() {
		return member;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @param member the member to set
	 */
	public void setMember(Member member) {
		this.member = member;
	}
	
	/**
	 * @param enabled the enabled to set
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	/**
	 * @param profile
	 */
	public void setProfile(Profile profile) {
		this.profile = profile;
	}
	
}
